package com.baiytfp.hf.HFDataExchange.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.common.db.QueryHelper;
//a
public class CommonUtil {
	
	public static boolean exceBath(String sql , Object[][] params) {
		Connection conn = null;
		try {
			conn= DBManager.getConnection();
			DBManager.beginTransaction(conn);
			QueryHelper.batch( conn, sql.toString(), params );
			DBManager.commitTransaction(conn);
			return true;
		} catch (SQLException e) {
			DBManager.rollbackTransaction(conn);
			e.printStackTrace();
			return false;
		}finally {
			DBManager.close(conn);
		}
	}
	
	
	public static final String RANDOM_CODE_KEY="$_RANDOM_CODE";
	
	public static boolean isEmpty(Object obj) {
		return null == obj;
	}
	
	public static boolean isEmpty(String str ) {
		return isEmpty( str ,true);
	}
	
	public static boolean isEmpty(String str ,boolean noBlank) {
		if( null == str ) return true;
		if( noBlank && 1 > str.trim().length() ) return true;
		return  str.length() == 0 ;
	}

	public static boolean isEmpty(Object[] array) {
		if( null == array ||  1 > array.length ) return true;
		return false;
	}

	public static boolean isEmpty(List<?> list) {
		if( null == list ||  1 > list.size() ) return true;
		return false;
	}
	
	public static boolean isEmpty(Map<?,?> map) {
		if( null == map || null == map.keySet() || 1 > map.keySet().size() ) return true;
		return false;
	}
	
	public static String[] split(String str, String separateStr) {
		if(null == str) return null;
		if(!isEmpty(separateStr) && str.contains(separateStr)) {
			return str.split(separateStr);
		}
		return new String[] {str};
	}
	
	
	public static JSONObject paseJson(HttpServletRequest request) throws Exception {
		java.io.BufferedReader br = null;
		StringBuffer json = new StringBuffer();
		try {
			br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
			String tmp = null;
			while(null != (tmp=br.readLine())) {
				json.append(tmp);
			}
		}catch (Exception e) {
			throw e;
		}finally {
			if(br!=null) br.close();
		}
		return JSONObject.parseObject(json.toString());
	}
	
	public static boolean matches(String pwd,String userCode) {
		int i = pwd.matches(".*\\d+.*") ? 1 : 0;

		//判断密码是否包含字母：包含返回1，不包含返回0
		int j = pwd.matches(".*[a-zA-Z]+.*") ? 1 : 0;

		//判断密码是否包含特殊符号(~!@#$%^&*()_+|<>,.?/:;'[]{}\)：包含返回1，不包含返回0
		int k = pwd.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*") ? 1 : 0;

		//判断密码长度是否在6-16位
		int l = pwd.length();

		//判断密码中是否包含用户名
		boolean contains = pwd.contains(userCode);

		if ( (i + j + k <= 2) || l < 8  || contains) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * 是否为ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request) {
		if(!isEmpty(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){  
            return true;  
        }
		return false;
	}
	
}
